package me.zhengjie.org.departmentbase.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.org.departmentbase.domain.DepartmentBase;
import me.zhengjie.org.departmentbase.service.DepartmentBaseService;
import me.zhengjie.org.departmentbase.service.dto.DepartmentBaseQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author ZGZ
* @date 2020-04-15
*/
@Api(tags = "部门基础信息管理")
@RestController
@RequestMapping("/api/departmentBase")
public class DepartmentBaseController {

    private final DepartmentBaseService departmentBaseService;

    public DepartmentBaseController(DepartmentBaseService departmentBaseService) {
        this.departmentBaseService = departmentBaseService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('departmentBase:list')")
    public void download(HttpServletResponse response, DepartmentBaseQueryCriteria criteria) throws IOException {
        departmentBaseService.download(departmentBaseService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询部门基础信息")
    @ApiOperation("查询部门基础信息")
    @PreAuthorize("@el.check('departmentBase:list')")
    public ResponseEntity<Object> getDepartmentBases(DepartmentBaseQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(departmentBaseService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增部门基础信息")
    @ApiOperation("新增部门基础信息")
    @PreAuthorize("@el.check('departmentBase:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody DepartmentBase resources){
        return new ResponseEntity<>(departmentBaseService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改部门基础信息")
    @ApiOperation("修改部门基础信息")
    @PreAuthorize("@el.check('departmentBase:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody DepartmentBase resources){
        departmentBaseService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除部门基础信息")
    @ApiOperation("删除部门基础信息")
    @PreAuthorize("@el.check('departmentBase:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        departmentBaseService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}